﻿using Data;
using DG.Tweening;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Level.Enemies
{
    public class BombVisual : MonoBehaviour
    {
        private readonly List<Tween> _tweens = new();
        [SerializeField] private BombHealth _health;
        [SerializeField] private BombDamage _damage;
        private GameSceneData _data;

        public void Construct(GameSceneData data) =>
            _data = data;

        private void Start() =>
            _health.Destroyed += OnBombDestroy;

        private void OnDestroy()
        {
            _health.Destroyed -= OnBombDestroy;
            foreach (var tween in _tweens.Where(tween => tween != null)) 
                tween.Kill();
        }

        private void OnBombDestroy()
        {
            var tweenX = transform.DOScaleX(_damage.Radius * 2,  _data.CreateAnimationTime  /  2).SetEase(Ease.InBounce)
                .OnComplete(() => 
                    transform.DOScaleX(0, _data.CreateAnimationTime / 2));
            _tweens.Add(tweenX);
            var tweenZ = transform.DOScaleZ(_damage.Radius * 2, _data.CreateAnimationTime / 2).SetEase(Ease.InBounce)
                .OnComplete(() => 
                    transform.DOScaleZ(0, _data.CreateAnimationTime / 2));
            _tweens.Add(tweenZ);
        }
    }
}